#include <jni.h>
#include "JniUtil.h"
#include "foundation.h"

using namespace NS_TIEC;

class JTiecodeFileObject : public TiecodeFileObject {
public:
  JTiecodeFileObject(JNIEnv* env, jobject java_obj) : env_(env), java_obj_(java_obj) {
    jclass klass = env->FindClass("com.ticode.compiler.api.file.TiecodeFileObject");
    INIT_JMETHOD(klass, GetName, getName, "()Ljava/lang/String;")
    INIT_JMETHOD(klass, LastModified, lastModified, "()J")
    INIT_JMETHOD(klass, ReadContent, readContent, "()Ljava/lang/String;")
    INIT_JMETHOD(klass, GetURI, getURI, "()Ljava/lang/String;")
    INIT_JMETHOD(klass, GetPath, getPath, "()Ljava/lang/String;")
    INIT_JMETHOD(klass, GetCategory, _getCategory, "()I")
    INIT_JMETHOD(klass, IsNameCompatible, _isNameCompatible, "(Ljava/lang/String;I)Z")
  }

  String GetName() override {
    jstring name = (jstring) env_->CallObjectMethod(java_obj_, JMETHOD(GetName));
    return env_->GetStringUTFChars(name, JNI_FALSE);
  }

  int64_t LastModified() override {
    jlong timestamp = env_->CallLongMethod(java_obj_, JMETHOD(LastModified));
    return timestamp;
  }

  String ReadContent() override {
    jstring content = (jstring) env_->CallObjectMethod(java_obj_, JMETHOD(ReadContent));
    return env_->GetStringUTFChars(content, JNI_FALSE);
  }

  String GetURI() override {
    jstring uri = (jstring) env_->CallObjectMethod(java_obj_, JMETHOD(GetURI));
    return env_->GetStringUTFChars(uri, JNI_FALSE);
  }

  String GetPath() override {
    jstring path = (jstring) env_->CallObjectMethod(java_obj_, JMETHOD(GetPath));
    return env_->GetStringUTFChars(path, JNI_FALSE);
  }

  FileCategory GetCategory() override {
    jint category = env_->CallIntMethod(java_obj_, JMETHOD(GetCategory));
    return (FileCategory) category;
  }

  bool IsNameCompatible(const String& name, FileCategory category) override {
    jboolean compatible = env_->CallBooleanMethod(java_obj_, JMETHOD(IsNameCompatible));
    return compatible;
  }

private:
  JNIEnv* env_;
  jobject java_obj_;
  DEF_JMETHOD(GetName)
  DEF_JMETHOD(LastModified)
  DEF_JMETHOD(ReadContent)
  DEF_JMETHOD(GetURI)
  DEF_JMETHOD(GetPath)
  DEF_JMETHOD(GetCategory)
  DEF_JMETHOD(IsNameCompatible)
};